package Datos;

import Entidades.Prestamos;
import Interfaces.IPrestamos;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Rafael Castelo
 */
public class DBPrestamos  implements IPrestamos{
    
    private Connection cn = new MiConexion().getConection();
    

    @Override
    public boolean ingresarprestamo(Prestamos nodo) {
        
    try {
            CallableStatement statement;
            statement = cn.prepareCall("{call agregaprestamo (?,?,?,?)}");
            statement.setInt(1, nodo.getnlb());
            statement.setInt(2, nodo.getidcliente());
            statement.setString(2,nodo.getfe() );
            statement.setString(3,nodo.getft());

            statement.executeUpdate();
        } catch (Exception ex) {
            //System.out.println("error al agregar calificaciones");
            ex.printStackTrace();
            return false;
        }
        return true;
    
    }

    @Override
    public boolean modificarprestamo(Prestamos nodo) {
     try {
            CallableStatement statement;
            statement = cn.prepareCall("{call modificprestamos (?,?,?,?,?,?)}");
            statement.setInt(1, nodo.getid());
            statement.setInt(2,nodo.getidcliente() );
            statement.setInt(2,nodo.getnlb() );
            statement.setString(4, nodo.getfe());
            statement.setString(5, nodo.getft());
            
            statement.executeUpdate();
        } catch (Exception ex) {
           //System.out.println("error al modificar calificaciones");
            ex.printStackTrace();
            return false;
        }
        return true;  
    }

    @Override
    public boolean eliminar(int id) {
    try {
            CallableStatement statement;
            statement = cn.prepareCall("{call eliminaprestamo (?)}");
            statement.setInt(1, id);
            statement.executeUpdate();
        } catch (Exception ex) {
            //System.out.println("error al eliminar calificacion");

            return false;
        }
        return true;
    }    
    

    @Override
    public ArrayList<Prestamos> consultafecha(String fecha) {
         ArrayList<Prestamos> per = new ArrayList<Prestamos>();
        try {
            CallableStatement statement = cn.prepareCall("{call listafecha(?)}");
            statement.setString(1, fecha);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Prestamos reglam = new Prestamos();

                reglam.setid(resultSet.getInt(1));
                reglam.setnlb(resultSet.getInt(2));
                reglam.setidcliente(resultSet.getInt(3));
                reglam.setft(resultSet.getString(5));
                

                per.add(reglam);

            }
            return per;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;

        }
    }

    @Override
    public ArrayList<Prestamos> consulnlbo(String nlibro) {
         ArrayList<Prestamos> per1 = new ArrayList<Prestamos>();
    try {
            CallableStatement statement = cn.prepareCall("{call listalibro (?)}");
            statement.setString(1, nlibro);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Prestamos reglam = new Prestamos();

                reglam.setid(resultSet.getInt(1));
                reglam.setfe(resultSet.getString(2));
                reglam.setidcliente(resultSet.getInt(3));
                reglam.setft(resultSet.getString(5));
                

                per1.add(reglam);

            }
            return per1;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;

        }    
    }

    @Override
    public ArrayList<Prestamos> consultaft(String ft) {
     ArrayList<Prestamos> per2 = new ArrayList<Prestamos>();    
      try {
            CallableStatement statement = cn.prepareCall("{call listaft (?)}");
            statement.setString(1, ft);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Prestamos reglam = new Prestamos();

                reglam.setid(resultSet.getInt(1));
                reglam.setnlb(resultSet.getInt(2));
                reglam.setidcliente(resultSet.getInt(3));
                reglam.setfe(resultSet.getString(5));
                

                per2.add(reglam);

            }
            return per2;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;

        }    
    }

    @Override
    public DefaultTableModel listaprestamos() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public ArrayList<Prestamos> listatodo() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
